﻿using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Reflection;
using System;

namespace dropbox_screen_shooter
{
    static class ScreenShooter
    {

        #region Events

        public delegate void ScreenEventHandler(ScreenEventArgs e);
        public static event ScreenEventHandler Screened;

        public class ScreenEventArgs : EventArgs
        {
            public string FileName { get; private set; }
            public string PasteLink { get; private set; }

            public ScreenEventArgs(string FileName, string PasteLink)
            {
                this.FileName = FileName;
                this.PasteLink = PasteLink;
            }
        }

        #endregion

        #region Methods

        public static void MakeFullScreen()
        {
            Rectangle bounds = Screen.GetBounds(Point.Empty);

            ImageFormat format = typeof(ImageFormat).GetProperty(Enum.GetName(typeof(Types), Options.Default.Format),
                        BindingFlags.Public | BindingFlags.Static).GetValue(null, null) as ImageFormat;

            string filename = DateTime.Now.ToString(Properties.Settings.Default.ScreenNameFormat) + "." + format.ToString();

            Bitmap b = new Bitmap(bounds.Width, bounds.Height);
            using (Graphics g = Graphics.FromImage(b))
                g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
            
            (b as Image).Save(Options.Default.Folder.FullName + filename, format);
            OnScreen(new ScreenEventArgs(filename, PasteLink(filename)));
        }

        public static void MakeWindowCopy(Rectangle WindowRect)
        {
            ImageFormat format = typeof(ImageFormat).GetProperty(Enum.GetName(typeof(Types), Options.Default.Format),
                        BindingFlags.Public | BindingFlags.Static).GetValue(null, null) as ImageFormat;

            string filename = DateTime.Now.ToString(Properties.Settings.Default.ScreenNameFormat) + "." + format.ToString();

            Bitmap b = new Bitmap(WindowRect.Width, WindowRect.Height);
            using (Graphics g = Graphics.FromImage(b))
            {
                g.CopyFromScreen(new Point(WindowRect.Left, WindowRect.Top), Point.Empty, WindowRect.Size);
            }   

            (b as Image).Save(Options.Default.Folder.FullName + filename, format);
            OnScreen(new ScreenEventArgs(filename, PasteLink(filename)));
        }

        private static void OnScreen(ScreenEventArgs e)
        {
            if (Screened != null)
                Screened(e);
        }

        private static string PasteLink(string filename)
        {
            if (Options.Default.Paste &&
                Options.Default.Folder.FullName.IndexOf(Properties.Settings.Default.PublicFolder) != -1)
            {
                string paste = Properties.Settings.Default.URL +
                    Options.Default.DbID +
                    Options.Default.Folder.FullName.Substring(
                    Options.Default.Folder.FullName.IndexOf(
                    Properties.Settings.Default.PublicFolder) +
                    Properties.Settings.Default.PublicFolder.Length) +
                    filename;

                paste = paste.Replace(@"\", @"/");

                Clipboard.SetText(paste);
                return paste;
            }
            return null;
        }

        #endregion
    }
}
